package cn.net.withub.dataCollector.web.service.impl;

import cn.net.withub.dataCollector.common.base.impl.BaseManagerImpl;
import cn.net.withub.dataCollector.common.model.TCollectorConfig;
import cn.net.withub.dataCollector.common.model.TCollectorContent;
import cn.net.withub.dataCollector.common.model.TCollectorData;
import cn.net.withub.dataCollector.common.model.TCollectorElement;
import cn.net.withub.dataCollector.common.utils.CommonProperties;
import cn.net.withub.dataCollector.common.utils.FileUtil;
import cn.net.withub.dataCollector.common.utils.HTMLSpirit;
import cn.net.withub.dataCollector.fastdfs.utils.FastDfsUtil;
import cn.net.withub.dataCollector.web.collector.CollectorReflect;
import cn.net.withub.dataCollector.web.collector.FtpUtil;
import cn.net.withub.dataCollector.web.collector.HttpClientUtils;
import cn.net.withub.dataCollector.web.service.CollectorService;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by yuanjie on 2018/4/27.
 */
@Service(value = "collectorService")
@Transactional(propagation = Propagation.REQUIRED)
public class CollectorServiceImpl extends BaseManagerImpl implements CollectorService {

    private Logger log = Logger.getLogger(this.getClass());

    @Resource(name="commonProperties")
    private CommonProperties commonProperties;

    /**
     * 保存对象
     * @param object
     * @throws Exception
     */
    @Transactional(propagation=Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor=Exception.class,readOnly = false)
    public void save(Object object)throws Exception{
        daoTool.saveOrUpdate(object);
    }

    /**
     *执行抓取
     * @throws Exception
     */
    public void executeCollector()throws Exception{
        List<TCollectorConfig> configList = findList(TCollectorConfig.class, " ifnull(qybz,0)=1");
        if(configList != null){
            for(TCollectorConfig config : configList){
                CollectorReflect.reflectUtil(config);
            }
        }
    }

    /**
     * 提取文本
     * @param data
     * @throws Exception
     */
    public void extractText(TCollectorData data) throws Exception{
        TCollectorContent content = null;
        content = (TCollectorContent) load(TCollectorContent.class," data_id='"+data.getId()+"'");
        if(content !=null && content.getContent()!=null && !content.getContent().trim().equals("")){
            content.setText(HTMLSpirit.getTextFromTHML(content.getContent()));

            daoTool.saveOrUpdate(content);
        }

        //log.info(data.getTitle()+"......提取文本完成！");
    }

    /**
     * 外网文件上传到fastdfs
     * @param t
     * @param <T>
     * @throws Exception
     */
    @Transactional(propagation=Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor=Exception.class)
    public <T> void uploadFastdfs(T t)throws Exception {
        String wjlj = "";//ftp文件路径
        String filePath = commonProperties.ftpDownloadPath;//ftp下载到本地目录
        String fileName = "";//文件名称
        TCollectorElement element = null;

        if(t instanceof TCollectorElement){
            element = (TCollectorElement)t;
        }
        if(element != null){
            //验证数据是否已经上传过
            element = (TCollectorElement)load(TCollectorElement.class," where id = '"+element.getId()+"'");
            if(element.getScbz()!=null && element.getScbz()==1){
                return;
            }

            wjlj = element.getWjlj();
        }else{
            log.info("数据对象为空，无需上传fastdfs......");
            return;
        }

        //获取文件名称
        fileName = wjlj.substring(wjlj.lastIndexOf("/")+1);

        //通过ftp下载文件
        FtpUtil.downloadFtpFile(wjlj,filePath,fileName);

        //上传文件到fastdfs
        String serverFilePath = FastDfsUtil.Upload(filePath+fileName);

        //更新上传标志和wjlj
        if(!serverFilePath.equals("")){
            if(element != null){
                element.setWjlj(serverFilePath);
                element.setScbz(1);
                daoTool.saveOrUpdate(element);

                //删除下载到本地的文件
                try{
                    FileUtil.deleteFile(filePath+fileName);
                }catch (Exception e){
                    log.info("删除【"+element.getId()+"】下载文件失败!");
                    e.printStackTrace();
                }

                //删除ftp文件
                FtpUtil.deleteFtp(wjlj,1);
            }
        }
    }

    public List<Map<String, Object>> getCollectorList(String dbTable, String whereFiled, String id) {
        List<Map<String, Object>> list = null;
        try {
            String sql = "SELECT * FROM "+dbTable+" WHERE "+whereFiled+" ='" + id + "' UNION SELECT * FROM data_collector_news."+dbTable+" WHERE "+whereFiled+" ='" + id + "';";
            list = jdbcTool.queryForList(sql);
        } catch (Exception e) {
            log.error("未获取到"+dbTable+"数据");
            return null;
        }
        return list;
    }
    public List<Map<String, Object>> getDataObjById(String dbTable, String where) {
        List<Map<String, Object>> list = null;
        try {
            //String sql = "SELECT * FROM "+dbTable+" WHERE "+whereFiled+" ='" + id + "' UNION SELECT * FROM data_collector_news."+dbTable+" WHERE "+whereFiled+" ='" + id + "';";
            String sql = "SELECT * FROM " + dbTable + " WHERE " + where;
            list = jdbcTool.queryForList(sql);
        } catch (Exception e) {
            log.error("未获取到" + dbTable + "数据");
            return null;
        }
        return list;
    }

    /**
     * 修改图片路径
     */
    public void saveImg() {
        Long time1 = System.currentTimeMillis();
        //String temp = "'http://149.22.0.204/upfiles/2013516162522817.jpg','http://149.4.0.202/upfiles/20160905105158520.jpg','http://149.4.0.202/upfiles/20170125154523541.jpg','http://149.16.0.202/upfiles/20163813529503.jpg','http://149.66.0.202/UpDocument/20080529/200805291118437616.files/image004.jpg','http://149.66.0.202/UpDocument/20120918/201209181609406525.files/image002.jpg','http://149.90.0.202/upfiles/20093199650303.jpg','http://149.54.0.202/upfiles/20131017165752622.jpg','http://149.60.0.202/UpDocument/20101112/201011120850022303.files/image002.jpg'";
        String sql = "SELECT url FROM `t_collector_element` WHERE wjlj like '%149.0.0.32%' limit 30000;";
        //String sql = "SELECT url FROM `t_collector_element` WHERE url in("+temp+") ";
        SqlRowSet rs = null;
        //String updateSql = "";
        //String serverFilePath = "";
        String url = "";
        //List<Map<String, Object>> list = null;
        try {
            rs = jdbcTool.queryForRowSet(sql);
            int i = 0;
            ExecutorService pool= Executors.newFixedThreadPool(30);
            while (rs.next()) {
                i++;
                url = rs.getString("url");
                pool.execute(new UpdataWjlj(url, i));
                /*serverFilePath = HttpClientUtils.getImage(url);
                log.info(i+"  --  "+url + "  --  " + serverFilePath);
                updateSql = "UPDATE  t_collector_element SET wjlj='" + serverFilePath + "' WHERE url='" + url + "'";
                jdbcTool.execute(updateSql);*/
            }
            pool.shutdown();
            try {
                // awaitTermination返回false即超时会继续循环，返回true即线程池中的线程执行完成主线程跳出循环往下执行，每隔10秒循环一次
                while (!pool.awaitTermination(10, TimeUnit.SECONDS)) ;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("异常url" + url);
        }
        Long time2 = System.currentTimeMillis();
        log.info("耗费时间:" + (time2 - time1) / 1000 + "秒");

    }

    public void updataContent() {
        List<Map<String, Object>> list = null;
        String dataId = "", content = "", text = "", updateSql = "";
        String sql="SELECT * from t_collector_content where content LIKE '%相关新闻%'  or CONTENT like '%wap_special%' ORDER BY SYS_TIME desc;";
        //String sql="SELECT * from t_collector_content where CONTENT like '%wap_special%' ORDER BY SYS_TIME desc limit 5;";
        //sql="SELECT data_id,content from t_collector_content where data_id='0e383489d-deda-4cf4-a43f-3b4ab6513eba';";
        try {
            list = jdbcTool.queryForList(sql);
            log.info(list.size());
            int i=0;
            for (Map<String, Object> map : list) {
                i++;
                dataId = map.get("data_id").toString();
                content = map.get("content").toString();
                Document doc = Jsoup.parse(content);
                Element element = doc.select("#article").get(0);
                if(content.contains("show_author")||content.contains("wap_special")){
                    //去掉专题
                    element.select(".wap_special").remove();
                    //去掉作者
                    element.select(".show_author").remove();
                }
                if(content.contains("<p>　　<a")){ //去掉相关新闻链接
                    Elements pstrong =element.select("p strong");
                    for (int j = 0, lenj = pstrong.size(); j < lenj; j++) {
                        Element e = pstrong.get(j).parent().nextElementSibling();
                        if (e == null) continue;
                        String temp = e.html();
                        if(temp.contains("</a>") || temp.contains("</strong>")){
                            pstrong.get(j).parent().remove();
                        }
                    }
                    while (element.select("p a").size()>0){
                        element.select("p a").get(0).parent().remove();
                    }
                }
                content = element.outerHtml();
                text = HTMLSpirit.getTextFromTHML(content);
                TCollectorContent tCollectorContent = (TCollectorContent) load(TCollectorContent.class, "data_id ='" + dataId + "'");
                tCollectorContent.setContent(content);
                tCollectorContent.setText(text);
                save(tCollectorContent);
                //content太乱   用springjdbc不好组装
                //updateSql = "UPDATE  t_collector_content SET content='" + content + "',text = '" + text + "' WHERE data_id='" + dataId + "'";
                //jdbcTool.execute(updateSql);
                log.info("更新成功"+i+"---"+dataId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误id:"+dataId);
        }

    }

    class UpdataWjlj extends Thread {
        private String url;
        private int index;

        public UpdataWjlj(String url, int index) {
            this.url = url;
            this.index = index;
        }
        public void run() {
            try {
                String serverFilePath = HttpClientUtils.getImage(url);
                log.info(index + "  --  " + url + "  --  " + serverFilePath);
                String updateSql = "UPDATE  t_collector_element SET wjlj='" + serverFilePath + "' WHERE url='" + url + "'";
                jdbcTool.execute(updateSql);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    public void saveTest(){
        String sql = "";
        try {
            sql="INSERT INTO t_collector_content(id,data_id,content,text,sys_time)values ('2','11','<a>sb</a>','sb','2018-02-28'); ";
            //sql="INSERT INTO dc.test values('语文','98')";
            jdbcTool.execute(sql);
            log.info("插入成功");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
